package test.taskengine;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.logicalcobwebs.proxool.ProxoolFacade;

import com.liusy.taskengine.InitialTaskEngine;
import com.liusy.taskengine.JobTask;
import com.liusy.taskengine.ReleaseTask;
import com.liusy.taskengine.jobmodel.DBJobModel;
import com.liusy.taskengine.jobmodel.IJobModel;
import com.liusy.taskengine.jobmodel.ListJobModel;
import com.liusy.taskengine.resultmodel.ResultModel;


/**
 * 任务引擎（门面）
 * 
 * @author liusy 2012-12-11
 */
public class StartTaskEngineMain {
	
	private static Logger log = Logger.getLogger(StartTaskEngineMain.class);
	
	private JobTask jt = null;
	
	
	public StartTaskEngineMain(){

	}
	
	
	//开始运行
	public void startRunTask() {
		try {
			
		//map任务 
		List<Map<String,Object>> mapTask=new ArrayList<Map<String,Object>>();
		File f = new File("C:\\Users\\Administrator\\Desktop\\1.txt");
		InputStream is = new FileInputStream(f);
		BufferedReader br = new BufferedReader(new InputStreamReader(is,"gbk"));
		String line =  br.readLine();
		while(line!= null){ 
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("name", line );
			mapTask.add(map);
			line = br.readLine();
		} 
		IJobModel listJobModel = new ListJobModel(mapTask,SqlTaskModel.class.getName());
		
		
		//初始化任务引擎
		InitialTaskEngine.initTaskEngine();
		
		System.out.println("Runtime.getRuntime().availableProcessors():"+Runtime.getRuntime().availableProcessors());
		//缓存中的任务少于400监听
		SqlCacheTaskCountListener sqlCacheTaskCountListener= new SqlCacheTaskCountListener(400);
		//任务池配置  //数据库任务
		IJobModel sjm = new DBJobModel("select value name from aa",500,SqlTaskModel.class.getName());
		//发布任务器             发布数据库任务池任务
		jt = new JobTask(sjm,new ResultModel("","D:\\result.txt"));
		//发布map任务 
		jt.addJobModel(listJobModel);
		//添加监听
		jt.AddReleaseTaskListener(ReleaseTask.CACHE_TASK_COUNT_LISTENER, sqlCacheTaskCountListener);
		
		jt.start();
		
		} catch (Exception e) {
			log.info(e.getMessage());
		}
		
	}
	//停止运行
	public void stopRunTask() {
		// TODO Auto-generated method stub
		ProxoolFacade.shutdown();
		jt.interrupt();
	}
	//暂停运行
	public void suspendRunTask() {
		jt.Suspend();

	}
	//恢复运行
	public void reRunTask() {
		jt.reRun();

	}

	
	
	
	
	
	public static void main(String[] args) {
		StartTaskEngineMain ste = new StartTaskEngineMain();
		ste.startRunTask();
	}

	
}
